package com.lc.cehui.ui.activity.home;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.CompanyMeetShowApi;
import com.lc.cehui.http.response.CompanyMeetShowBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.ui.adapter.TabViewPagerAdapter;
import com.lc.cehui.ui.fragment.ExhibitionInformationFragment;
import com.lc.cehui.ui.fragment.home.ExhibitionIntroduceFragment;
import com.lc.cehui.ui.fragment.home.ExhibitionRangeFragment;
import com.lc.cehui.ui.fragment.home.ExhibitionatlasFragment;
import com.lc.cehui.util.CourseUtils;
import com.lc.cehui.util.SPUtil;
import com.lc.cehui.widget.SlidingTabLayout;
import com.lc.cehui.widget.XCollapsingToolbarLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ExhibitionDetailsActivity extends AppActivity implements XCollapsingToolbarLayout.OnScrimsListener{
    @BindView(R.id.img_logo)
    ImageView imgLogo;
    @BindView(R.id.img_company_logo)
    ImageView imgCompanyLogo;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_tag)
    TextView tvTag;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_zhuban)
    TextView tvZhuban;
    @BindView(R.id.tv_zhanhuishijian)
    TextView tvZhanhuishijian;
    @BindView(R.id.tv_kaifangshijian)
    TextView tvKaifangshijian;
    @BindView(R.id.tv_jubanchengshi)
    TextView tvJubanchengshi;
    @BindView(R.id.tv_type)
    TextView tvType;

    @BindView(R.id.tb_home_title)
    Toolbar tbHomeTitle;
    @BindView(R.id.ctl_home_bar)
    XCollapsingToolbarLayout mCollapsingToolbarLayout;
    @BindView(R.id.slidingTabLayout)
    SlidingTabLayout slidingTabs;
    @BindView(R.id.vp)
    ViewPager vp;
    private List<Fragment> fragments = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    private CompanyMeetShowBean data;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_exhibitiondetails;
    }

    @Override
    protected void initView() {
        titles.add("展会介绍");
        titles.add("历届图集");
        titles.add("展品范围");
        titles.add("展馆信息");

        company_meet_show();
        //设置渐变监听
        mCollapsingToolbarLayout.setOnScrimsListener(this);

    }

    private void company_meet_show() {
        EasyHttp.post(this)
                .api(new CompanyMeetShowApi().setInfo_id(getIntent().getStringExtra("info_id")))
                .request(new HttpCallback<String>(this) {

                    @Override
                    public void onSucceed(String datas) {
                        SPUtil.remove(ExhibitionDetailsActivity.this, IntentKey.TEXT);
                        SPUtil.put(ExhibitionDetailsActivity.this, IntentKey.TEXT, datas);
                        Gson gson = new Gson();
//                        将存储的数据转成实体类
                        data = gson.fromJson(datas, new TypeToken<CompanyMeetShowBean>() {
                        }.getType());
                        GlideApp.with(ExhibitionDetailsActivity.this).load(data.getData().getCompany_picurl()).apply(new RequestOptions().placeholder(R.mipmap.logo_qyzp).error(R.mipmap.logo_qyzp)).into((ImageView)imgCompanyLogo);
                        tvName.setText(data.getData().getCompany_name());
                        tvTag.setText(data.getData().getCity()+"  ｜  "+ data.getData().getDistrict()+"  ｜  "+ data.getData().getCompany_scale());
                        tvType.setText(data.getData().getExhibition_type());
                        tvAddress.setText(data.getData().getCompany_city()+ data.getData().getCity()+ data.getData().getCompany_district()+ data.getData().getAddress());

                        tvZhuban.setText("主办单位      "+data.getData().getExhibition_company());
                        tvZhanhuishijian.setText("展会时间      "+data.getData().getExhibition_time());
                        tvKaifangshijian.setText("开放时间      "+CourseUtils.getMillsToTime(data.getData().getCreate_time()));
                        tvJubanchengshi.setText("举办城市      "+data.getData().getExhibition_city());

                        fragments.add(new ExhibitionIntroduceFragment());
                        fragments.add(new ExhibitionatlasFragment());
                        fragments.add(new ExhibitionRangeFragment());
                        fragments.add(new ExhibitionInformationFragment());
                        vp.setAdapter(new TabViewPagerAdapter(ExhibitionDetailsActivity.this.getSupportFragmentManager(), ExhibitionDetailsActivity.this, fragments, titles));
                        slidingTabs.setTabTitleTextSize(13);//标题字体大小
                        slidingTabs.setTitleTextColor(Color.parseColor("#028BFF"), Color.parseColor("#999999"));//标题字体颜色
                        slidingTabs.setTabStripWidth(60);//滑动条宽度
                        slidingTabs.setSelectedIndicatorColors(Color.parseColor("#028BFF"));//滑动条颜色
                        slidingTabs.setDistributeEvenly(true); //均匀平铺选项卡
                        slidingTabs.setViewPager(vp);//最后调用此方法
                        vp.setOffscreenPageLimit(fragments.size());//fragment保存的个数
                        vp.setCurrentItem(0);//设置卡在页面

                    }
                });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.img_daohang, R.id.img_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_daohang:
                break;
            case R.id.img_back:
                finish();
                break;
        }
    }

    @Override
    public void onScrimsStateChange(XCollapsingToolbarLayout layout, boolean shown) {
        if (shown) {
            tbHomeTitle.setBackgroundResource(R.color.main_color);
//
        } else {
            tbHomeTitle.setBackgroundResource(R.color.black100);

//
        }
    }
}
